using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Profile;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DoctorsDSTableAdapters;
using System.Diagnostics;

public partial class ProfileInfoPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Button btnDoctorSave = (Button)dvDoctors.FindControl("btnSave");
            Button btnNurseSave = (Button)dvNurses.FindControl("btnSave");
            Button btnAttendantSave = (Button)dvAttendants.FindControl("btnSave");

            btnDoctorSave.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(btnDoctorSave, string.Empty));
            btnNurseSave.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(btnNurseSave, string.Empty));
            btnAttendantSave.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(btnAttendantSave, string.Empty));

            if (Request.UrlReferrer != null)
            {
                

                //set loginPageRelativeUrl according to application path
                //if the website is loaded with visual studio path will be different than IIS
                string loginPageRelativeUrl = string.Empty;
                if (Request.ApplicationPath.Equals("/"))
                {
                    loginPageRelativeUrl = "/Login.aspx";
                }
                else
                {
                    loginPageRelativeUrl = Request.ApplicationPath + "/Login.aspx";
                }

                if (Request.UrlReferrer.AbsolutePath.Equals(loginPageRelativeUrl))
                {
                    SetViewAccordingToRole();
                }
                else
                {
                    mvUserInfo.SetActiveView(vError);
                }
            }
            else
            {
                mvUserInfo.SetActiveView(vError);
            }
        }
    }
    private void SetViewAccordingToRole()
    {
        string userName = string.Empty;
        if (Request.QueryString["username"] != null)
        {
            userName = Request.QueryString["username"];
            if (Roles.IsUserInRole(userName, ClinicUserRoles.Doctors.ToString()))
            {
                mvUserInfo.SetActiveView(vDoctor);
            }
            else if(Roles.IsUserInRole(userName, ClinicUserRoles.Nurses.ToString()))
            {
                mvUserInfo.SetActiveView(vNurse);
            }
            else if (Roles.IsUserInRole(userName, ClinicUserRoles.Attendants.ToString()))
            {
                mvUserInfo.SetActiveView(vAttendant);
            }
            else
            {
                mvUserInfo.SetActiveView(vInsufficientRights);
            }
        }
    }
    
    protected void btnSave_Click(object sender, CommandEventArgs e)
    {
        DetailsView currentDetailsView = null;
        if (e.CommandArgument.ToString() == "Doctor")
        {
            currentDetailsView = dvDoctors;
        }
        else if (e.CommandArgument.ToString() == "Nurse")
        {
            currentDetailsView = dvNurses;
        }
        else if (e.CommandArgument.ToString() == "Attendant")
        {
            currentDetailsView = dvAttendants;
        }
        
        currentDetailsView.InsertItem(true);
        
        Response.Redirect("~/Default.aspx");        
    }

    protected void dvItem_Inserting(object sender, DetailsViewInsertEventArgs e)
    {
        string userName = string.Empty;
        if (Request.QueryString["username"] != null)
        {
            userName = Server.HtmlEncode(Request.QueryString["username"]);
        }

        MembershipUser user = Membership.GetUser(userName);
        if (user != null)
        {
            e.Values.Add("UserId", user.ProviderUserKey);
        }
    }
    protected void dvDoctors_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
    {
        Guid providerUserKey = (Guid)e.Values["UserId"];
        MembershipUser user = Membership.GetUser(providerUserKey);
        if (user != null)
        {
            Cache[user.UserName] = true;
        }
    }
}
